package com.xfcycle.zebrabikegp.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.xfcycle.zebrabikegp.R;
import com.xfcycle.zebrabikegp.constant.BluetoothDeviceConstant;
import com.xfcycle.zebrabikegp.constant.ToastConstant;
import com.xfcycle.zebrabikegp.util.HandleDataByBLEUtil;
import com.xfcycle.zebrabikegp.util.HandleDataBySharePreferencesUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 输出功率设置
 */
public class BsGearPowerOutputActivity extends RootActivity {

    private static final String TAG = "BsGearPowerOutput";

    private ImageView iv_gear_power_output_back;

    private LinearLayout ll_power_limit_e;

    private SeekBar sb_gear_power_e;
    private SeekBar sb_gear_power_p;

    private TextView tv_gear_power_e_percent;
    private TextView tv_gear_power_p_percent;

    private HandleDataByBLEUtil handleDataByBLEUtil;
    private HandleDataBySharePreferencesUtil handleDataBySharePreferencesUtil;

    private GearPowerOutputHandleDataFromBleReceiver gearPowerOutputHandleDataFromBleReceiver;

    //拖动之前的 e档 值
    private Integer eAmount;
    //拖动之前的 p档 值
    private Integer pAmount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bs_gear_power_output);

        initView();
        initViewListener();
    }

    @Override
    protected void onResume() {
        super.onResume();

        //注册广播
        gearPowerOutputHandleDataFromBleReceiver = new GearPowerOutputHandleDataFromBleReceiver();
        registBroadcastReceiverAtActivity(gearPowerOutputHandleDataFromBleReceiver, BluetoothDeviceConstant.GET_DATA_FROM_BLUETOOTH_BROADCAST_RECEIVER_ACTION);

        setViewContent();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //回收广播接收者
        unregisterReceiver(gearPowerOutputHandleDataFromBleReceiver);
    }

    /***********************************************************************************************/
    /**                                 初始化控件对象 监听 控件内容方法                             **/
    /***********************************************************************************************/

    private void initView() {
        iv_gear_power_output_back = (ImageView) findViewById(R.id.iv_gear_power_output_back);

        ll_power_limit_e = (LinearLayout) findViewById(R.id.ll_power_limit_e);

        sb_gear_power_e = (SeekBar) findViewById(R.id.sb_gear_power_e);
        sb_gear_power_p = (SeekBar) findViewById(R.id.sb_gear_power_p);

        tv_gear_power_e_percent = (TextView) findViewById(R.id.tv_gear_power_e_percent);
        tv_gear_power_p_percent = (TextView) findViewById(R.id.tv_gear_power_p_percent);

        handleDataByBLEUtil = HandleDataByBLEUtil.getInstance(this, -1);
        handleDataBySharePreferencesUtil = HandleDataBySharePreferencesUtil.getInstance(this);
    }

    private void initViewListener() {
        //回退事件监听
        iv_gear_power_output_back.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                closeActivity();
            }
        });

        //设置ECO模式下的输出功率
        sb_gear_power_e.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                String percent = seekBar.getProgress() + "";

                if (null == percent) {
                    Log.e(TAG, "onStopTrackingTouch: seekBar e 的百分比为空");
                    return;
                }

                tv_gear_power_e_percent.setText(percent + "%");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                eAmount = seekBar.getProgress();
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                String percent = seekBar.getProgress() + "";

                if (null == percent) {
                    Log.e(TAG, "onStopTrackingTouch: seekBar e 的百分比为空");
                    return;
                }

                handleDataByBLEUtil.sendDataToDevice("s|2,e," + percent);

                Map<String, String> progress = new HashMap<>();
                progress.put("gear_power_e_percent", percent);
                handleDataBySharePreferencesUtil.putDateIntoSharePre(APP_INFO_SP_NAME, progress);
            }
        });

        //设置POW模式下的输出功率
        sb_gear_power_p.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                String percent = seekBar.getProgress() + "";

                if (null == percent) {
                    Log.e(TAG, "onStopTrackingTouch: seekBar p 的百分比为空");
                    return;
                }

                tv_gear_power_p_percent.setText(percent + "%");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                pAmount = seekBar.getProgress();
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                String percent = seekBar.getProgress() + "";

                if (null == percent) {
                    Log.e(TAG, "onStopTrackingTouch: seekBar p 的百分比为空");
                    return;
                }

                handleDataByBLEUtil.sendDataToDevice("s|2,p," + percent);

                Map<String, String> progress = new HashMap<>();
                progress.put("gear_power_p_percent", percent);
                handleDataBySharePreferencesUtil.putDateIntoSharePre(APP_INFO_SP_NAME, progress);
            }
        });
    }

    private void setViewContent() {
//        if(null != BlueToothAssist.connectDeviceName && BlueToothAssist.connectDeviceName.contains("vta")){
//            //当前连接的是Volta
//            ll_power_limit_e.setVisibility(View.VISIBLE);
//        }else {
//            ll_power_limit_e.setVisibility(View.GONE);
//        }

        handleDataByBLEUtil.sendDataToDevice("s|2,r");
    }

    /***********************************************************************************************/
    /**                                 自定义广播接收者类                                          **/
    /***********************************************************************************************/

    /**
     * 广播接收者类，负责接收从蓝牙发出的数据并对其进行处理
     */
    class GearPowerOutputHandleDataFromBleReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String result = intent.getStringExtra("result");

            //修改e档输出功率成功
            if (result.contains("^s|2,e")) {
                Toast.makeText(context.getApplicationContext(), ToastConstant.MODIFY_GEAR_POWER_SUCCESS, Toast.LENGTH_SHORT).show();
                return;
            }

            //修改p档输出功率成功
            if (result.contains("^s|2,p")) {
                Toast.makeText(context.getApplicationContext(), ToastConstant.MODIFY_GEAR_POWER_SUCCESS, Toast.LENGTH_SHORT).show();
                return;
            }

            //初始化数据
            if (result.contains("^s|2,r")) {
                String[] outPut = result.replace("^s|2,r,", "").replace("$", "").split(",");
                sb_gear_power_e.setProgress(Integer.parseInt(outPut.length > 0 ? outPut[0] : "0"));
                sb_gear_power_p.setProgress(Integer.parseInt(outPut.length > 1 ? outPut[1] : "0"));
                return;
            }

            //和设备交互失败 修改输出功率至初始状态
            if (null == eAmount) {
                Log.e(TAG, "onReceive: e档输出功率为null");
            } else {
                sb_gear_power_e.setProgress(eAmount);
            }

            if (null == pAmount) {
                Log.e(TAG, "onReceive: e档输出功率为null");
            } else {
                sb_gear_power_p.setProgress(pAmount);
            }
            Toast.makeText(context.getApplicationContext(), ToastConstant.MODIFY_GEAR_POWER_FAILURE, Toast.LENGTH_SHORT).show();
        }
    }
}
